home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / win / whttpd14.zip / SUPPORT / HTPASSWD / HTPASSWD.C < prev    next >
C/C++ Source or Header  |  1994-03-21  |  5KB  |  212 lines

  1. /*
  2.  * htpasswd.c: simple program for manipulating password file for NCSA httpd
  3.  * 
  4.  * Rob McCool
  5.  *
  6.  * DOS version (for Windows & NT) by Bob Denny (denny@netcom.com)
  7.  * Developed under Borland C++ V4 (Windows) with DOS target. Asserts
  8.  * that the Borland compiler is being used.
  9.  */
  10.  
  11.  
  12. #include <sys/types.h>
  13. #include <stdio.h>
  14. #include <string.h>
  15.  
  16. //(rbd)+
  17. #if defined __MSDOS__
  18. #  if !defined __BORLANDC__
  19. #    error This may not work on non-Borland compiler(s)
  20. #  else
  21. #    include <signal.h>
  22. #    include <conio.h>
  23. #  endif
  24. #else
  25. #  include <signal.h>
  26. #endif
  27. //(rbd)-
  28.  
  29. #define LF 10
  30. #define CR 13
  31.  
  32. #define MAX_STRING_LEN 256
  33.  
  34. char *tn;
  35.  
  36. char *strd(char *s) {
  37.     char *d;
  38.  
  39.     d=(char *)malloc(strlen(s) + 1);
  40.     strcpy(d,s);
  41.     return(d);
  42. }
  43.  
  44. void getword(char *word, char *line, char stop) {
  45.     int x = 0,y;
  46.  
  47.     for(x=0;((line[x]) && (line[x] != stop));x++)
  48.         word[x] = line[x];
  49.  
  50.     word[x] = '\0';
  51.     if(line[x]) ++x;
  52.     y=0;
  53.  
  54.     while(line[y++] = line[x++]);
  55. }
  56.  
  57. int getline(char *s, int n, FILE *f) {
  58.     register int i=0;
  59.  
  60.     while(1) {
  61.         s[i] = (char)fgetc(f);
  62.  
  63.         if(s[i] == CR)
  64.             s[i] = fgetc(f);
  65.  
  66.         if((s[i] == 0x4) || (s[i] == LF) || (i == (n-1))) {
  67.             s[i] = '\0';
  68.             return (feof(f) ? 1 : 0);
  69.         }
  70.         ++i;
  71.     }
  72. }
  73.  
  74. void putline(FILE *f,char *l) {
  75.     int x;
  76.  
  77.     for(x=0;l[x];x++) fputc(l[x],f);
  78.     fputc('\n',f);
  79. }
  80.  
  81.  
  82. /* From local_passwd.c (C) Regents of Univ. of California blah blah */
  83. static unsigned char itoa64[] =         /* 0 ... 63 => ascii - 64 */
  84.         "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  85.  
  86. to64(s, v, n)
  87.   register char *s;
  88. #if defined __MSDOS__                                                    // (rbd)+
  89.   register int v;                        // rand() returns int
  90. #else                                                                    // (rbd)-
  91.   register long v;
  92. #endif                                                                    // (rbd)
  93.   register int n;
  94. {
  95.     while (--n >= 0) {
  96.         *s++ = itoa64[v&0x3f];
  97.         v >>= 6;
  98.     }
  99. }
  100.  
  101. #if !defined __MSDOS__                                                    // (rbd)
  102. char *getpass(char *prompt); /* ugh */
  103. #endif                                                                    // (rbd)
  104. char *crypt(char *pw, char *salt); /* why aren't these prototyped in include */
  105.  
  106.  
  107. void add_password(char *user, FILE *f) {
  108.     char *pw, *cpw, salt[3];
  109. #if defined __MSDOS__                                                    // (rbd)+
  110.     pw = strdup((char *)getpass("New password:"));
  111. #else                                                                    // (rbd)-
  112.     pw = strd(getpass("New password:"));
  113. #endif                                                                    // (rbd)
  114.     if(strcmp(pw, (char *)getpass("Re-type new password:"))) {            // (rbd)
  115.         fprintf(stderr,"They don't match, sorry.\n");
  116.         if(tn)
  117.             unlink(tn);
  118.         exit(1);
  119.     }
  120.  
  121.     (void)srand((int)time((time_t *)NULL));
  122.     to64(&salt[0], rand(), 2);
  123.     cpw = crypt(pw,salt);
  124.     free(pw);
  125.     fprintf(f,"%s:%s\n",user,cpw);
  126. }
  127.  
  128. void usage() {
  129.     fprintf(stderr,"Usage: htpasswd [-c] passwordfile username\n");
  130.     fprintf(stderr,"The -c flag creates a new file.\n");
  131.     exit(1);
  132. }
  133.  
  134. void interrupted() {
  135.     fprintf(stderr,"Interrupted.\n");
  136.     if(tn) unlink(tn);
  137.     exit(1);
  138. }
  139.  
  140. main(int argc, char *argv[]) {
  141.     FILE *tfp,*f;
  142.     char user[MAX_STRING_LEN];
  143.     char line[MAX_STRING_LEN];
  144.     char l[MAX_STRING_LEN];
  145.     char w[MAX_STRING_LEN];
  146.     char command[MAX_STRING_LEN];
  147.     int found;
  148.  
  149.     tn = NULL;
  150.     signal(SIGINT,(void (*)())interrupted);
  151.     if(argc == 4) {
  152.         if(strcmp(argv[1],"-c"))
  153.             usage();
  154.         if(!(tfp = fopen(argv[2],"w"))) {
  155.             fprintf(stderr,"Could not open passwd file %s for writing.\n",
  156.                     argv[2]);
  157.             perror("fopen");
  158.             exit(1);
  159.         }
  160.         printf("Adding password for %s.\n",argv[3]);
  161.         add_password(argv[3],tfp);
  162.         fclose(tfp);
  163.         exit(0);
  164.     } else if(argc != 3) usage();
  165.  
  166.     tn = tmpnam(NULL);
  167.     if(!(tfp = fopen(tn,"w"))) {
  168.         fprintf(stderr,"Could not open temp file.\n");
  169.         exit(1);
  170.     }
  171.  
  172.     if(!(f = fopen(argv[1],"r"))) {
  173.         fprintf(stderr,
  174.                 "Could not open passwd file %s for reading.\n",argv[1]);
  175.         fprintf(stderr,"Use -c option to create new one.\n");
  176.         exit(1);
  177.     }
  178.     strcpy(user,argv[2]);
  179.  
  180.     found = 0;
  181.     while(!(getline(line,MAX_STRING_LEN,f))) {
  182.         if(found || (line[0] == '#') || (!line[0])) {
  183.             putline(tfp,line);
  184.             continue;
  185.         }
  186.         strcpy(l,line);
  187.         getword(w,l,':');
  188.         if(strcmp(user,w)) {
  189.             putline(tfp,line);
  190.             continue;
  191.         }
  192.         else {
  193.             printf("Changing password for user %s\n",user);
  194.             add_password(user,tfp);
  195.             found = 1;
  196.         }
  197.     }
  198.     if(!found) {
  199.         printf("Adding user %s\n",user);
  200.         add_password(user,tfp);
  201.     }
  202.     fclose(f);
  203.     fclose(tfp);
  204. #if defined __MSDOS__                                                    // (rbd)+
  205.     sprintf(command, "copy %s %s", tn, argv[1]);
  206. #else                                                                    // (rbd)-
  207.     sprintf(command,"cp %s %s",tn,argv[1]);
  208. #endif                                                                    // (rbd)
  209.     system(command);
  210.     unlink(tn);
  211. }
  212.